package DTO;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import org.Globals;
import org.Utility;
import org.sqlStatements;

import CustomUI.LayerView;
import GameTools.ydPoint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.View;

public class DrawingCmdDTO extends BaseDTO {
	
	static SQLiteDatabase db=Globals.sqlDB;
	public int dcid=-1;
	public int lid;
	public ArrayList<ydPoint> coordinates;
	public String command;
	public int instructNum;
	public Bitmap image=null;
	public int mid;
	
	Properties properties;
	
	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties=new Properties(properties);
	}

	public static Context mContext;
	
	public DrawingCmdDTO(int lid, int instructNum, SQLiteDatabase db) {
		DrawingCmdDTO.db=db;
		this.lid=lid;
		this.instructNum=instructNum;
		
		command=sqlStatements.DC_CREATE_LAYER;
		
		coordinates=new ArrayList<ydPoint>();
		coordinates.add(new ydPoint());
		
		Bitmap bitmap = Bitmap.createBitmap(100,100, Bitmap.Config.RGB_565);
		byte[] img=Utility.bitmaptoByteArray(bitmap);
		ContentValues values = new ContentValues();
		
		values.put(sqlStatements.LID,lid);
		values.put(sqlStatements.INSTRUCT_NUM,instructNum);
		values.put(sqlStatements.IMAGE,img);
		values.put(sqlStatements.COMMAND, command);
		values.put(sqlStatements.COORDINATES, ydPoint.arrayToString(coordinates));
		dcid = (int) db.insert(sqlStatements.DRAWCMD, null, values);
		return;
	}

	public DrawingCmdDTO() {
		
	}

	public int getDcid() {
		return dcid;
	}

	public void setDcid(int dcid) {
		this.dcid = dcid;
	}

	public int getLid() {
		return lid;
	}

	public void setLid(int lid) {
		this.lid = lid;
	}

	public ArrayList<ydPoint> getCoordinates() {
		return coordinates;
	}

	public void setCoordinates(ArrayList<ydPoint> coordinates) {
		this.coordinates = coordinates;
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public int getInstructNum() {
		return instructNum;
	}

	public void setInstructNum(int instructNum) {
		this.instructNum = instructNum;
	}

	public Bitmap getImage() {
		return image;
	}

	public void setImage(Bitmap image) {
		this.image= Bitmap.createBitmap(image);
	}

	public int getMid() {
		return mid;
	}

	public void setMid(int mid) {
		this.mid = mid;
	}

	public static Context getmContext() {
		return mContext;
	}

	public static void setmContext(Context mContext) {
		DrawingCmdDTO.mContext = mContext;
	}

	public void draw(LayerView lV) {
	}

	public int insertUpdate() {		
		if(dcid!=-1)
			return dcid;
		Bitmap bitmap = Bitmap.createBitmap(100,100, Bitmap.Config.RGB_565);
		
		byte[] img;
		if(image==null)
			img=Utility.bitmaptoByteArray(bitmap);
		else
			img=Utility.bitmaptoByteArray(image);
		ContentValues values = new ContentValues();
		
		values.put(sqlStatements.LID,lid);
		values.put(sqlStatements.INSTRUCT_NUM,instructNum);
		values.put(sqlStatements.IMAGE,img);
		values.put(sqlStatements.COMMAND, command);
		values.put(sqlStatements.COORDINATES, ydPoint.arrayToString(coordinates));
		
		try{
		dcid = (int) db.insert(sqlStatements.DRAWCMD, null, values);
		
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		return dcid;
		
	}

	public static void deleteAndAfter(int lid, int instructNum) {
		String sql ="Delete from "+ sqlStatements.LAYERS
				+ " Where " + sqlStatements.LID + "= " + lid
				+ " and (Select count(*) from "
				+ sqlStatements.DRAWCMD + " Where "
				+ sqlStatements.INSTRUCT_NUM + ">= "+ instructNum + " and "
				+ sqlStatements.LID + "= " + lid
				+ " and " + sqlStatements.COMMAND + " = '"+ sqlStatements.DC_CREATE_LAYER + "')>=1";
		int i = db.delete(sqlStatements.LAYERS, sqlStatements.LID + "= " + lid
				+ " and (Select count(*) from "
				+ sqlStatements.DRAWCMD + " Where "
				+ sqlStatements.INSTRUCT_NUM + ">= "+ instructNum + " and "
				+ sqlStatements.LID + "= " + lid
				+ " and " + sqlStatements.COMMAND + " = '"+ sqlStatements.DC_CREATE_LAYER + "')>=1", null);
		db.delete(sqlStatements.DRAWCMD, sqlStatements.LID + "=? and "
				+ sqlStatements.INSTRUCT_NUM + ">=?", new String[]{lid+"", instructNum+""});
		
	Log.d("ASDS", i + "");
	}

	public static ArrayList<DrawingCmdDTO> getDrawingCmds(int lid, SQLiteDatabase sqlDB) {
		ArrayList<DrawingCmdDTO> results=new ArrayList<DrawingCmdDTO>();
		
		try {
			Cursor c = null;
			c = sqlDB.query(sqlStatements.DRAWCMD,
					new String[]{sqlStatements.DCID, sqlStatements.COORDINATES, sqlStatements.COMMAND,
					sqlStatements.INSTRUCT_NUM, sqlStatements.IMAGE }, 
					sqlStatements.LID+"=?", 
					new String[]{lid+""},
					"", "", sqlStatements.DCID);

			if (c != null) {
				DrawingCmdDTO d; 
				while (c.moveToNext()) {
					d=new DrawingCmdDTO();
					d.setLid(lid);
					d.setDcid(c.getInt(0));
					d.setCoordinates(ydPoint.StringToarray(c.getString(1)));
					d.setCommand(c.getString(2));
					d.setInstructNum(c.getInt(3));
					byte[] pic = (c.getBlob(4));
					ByteArrayInputStream imageStream = new ByteArrayInputStream(
							pic);
					d.setImage(BitmapFactory.decodeStream(imageStream));
					results.add(d);
				}
			} else {
				Log.d("",
						"== something happened, could not extract from database");
			}
			if (c != null) {
				c.close();
			} else {
				Log.d("", "== c was null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.d("", "== something went wrong");
		}
		return results;
	}

}
